package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum FanState implements EnumTrans<CompleteEntity.FanState, FanState> {

    /**
     * 默认属性
     * 正常
     * 异常
     */
    FanStateDefault(CompleteEntity.FanState.FanStateDefault),
    NORMAL(CompleteEntity.FanState.normal),
    ERROR(CompleteEntity.FanState.error),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.FanState fanState;

    FanState(CompleteEntity.FanState fanState) {
        this.id = fanState.getNumber();
        this.name = fanState.name();
        this.fanState = fanState;
    }

    /**
     * @param fanState
     * @return 对应的类型
     */
    public static FanState getFanStateStruct(CompleteEntity.FanState fanState) {
        Objects.requireNonNull(fanState, "fanState can not be null.");
        return Arrays.stream(FanState.values())
                .filter(e -> e.name.equals(fanState.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.FanState toProto() {
        return this.fanState;
    }

    @Override
    public FanState formProto(CompleteEntity.FanState fanState) {
        return getFanStateStruct(fanState);
    }
}

